var app = getApp();
const requireHttps = app.globalData.requireHttps //获取require域名
//默认选择充值5元
app.globalData.moeny = '5'
Page({
  data: {
    data_num: 1,
    orderMoney: '其他金额',
    item: [
      '5',
      '10',
      '20',
      '50',
      '100'
    ]
  },
  onLoad: function() {
    let _this = this;

    _this.getRecharge();
  },
  addActive: function(e) {
    // console.log(e.currentTarget.dataset.money)
    app.globalData.moeny = e.currentTarget.dataset.money
    let _this = this;
    _this.setData({
      data_num: e.currentTarget.dataset.num
    })
  },
  //创建充值订单
  recharge: () => {
    console.log(app.globalData.moeny);
    if (app.globalData.moeny == undefined || app.globalData.moeny == null || app.globalData.moeny == '') {
      wx.showModal({
        title: '提示',
        content: '请选择你需要充值的金额',
        success: res => {
          if (res.confirm) {
            console.log('用户选择确定')
          } else if (res.cancel) {
            console.log('用户选择取消')
          }
        }
      })
    } else {
      console.log('开始创建充值订单')
      //开始创建充值订单
      // console.log(app.globalData.token + '-' + app.globalData.moeny + '-' + app.globalData.openid);
      wx.request({
        url: '' + requireHttps + '/api/pay/createRechargeOrder',
        data: {
          token: app.globalData.token,
          money: app.globalData.moeny,
          openid: app.globalData.openid
        },
        method: 'post',
        success: res => {
          //存储微信支付需要的参数
          app.globalData.timeStamp = res.data.data.timeStamp
          app.globalData.nonceStr = res.data.data.nonceStr
          app.globalData.package = res.data.data.package
          app.globalData.paySign = res.data.data.paySign
          //发起微信支付
          wx.requestPayment({
            timeStamp: app.globalData.timeStamp,
            nonceStr: app.globalData.nonceStr,
            package: app.globalData.package,
            signType: 'MD5',
            paySign: app.globalData.paySign,
            success: res => {
              if (res.errMsg == 'requestPayment:ok'){
                wx.showToast({
                  title: '充值成功!',
                })
                //支付成功跳转到个人中心
                wx.navigateBack({
                  delta: 1
                })
              } else {
                //取消支付
                console.log('用户取消支付')
              }
            },
            fail: res => {  //支付失败
              return false;
            }
          })
        }
      })
    }
  },
  getMoney: function(e) {
    //存储文本输出money
    app.globalData.moeny = e.detail.value;
  },
  getRecharge: function() {
    let _this = this
    wx.request({
      url: '' + requireHttps + '/api/publics/getSystemConfig',
      data: {},
      method: 'post',
      success: res => {
        _this.setData({
          rechargeDetail: res.data.data
        })
      }
    })
  }
})